//
//  CoverStyleViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/19.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "CoverStyleViewController.h"

#import "SelectDesignerViewController.h"
#import "EvaluateViewController.h"
#import "FPCustomButton.h"

@interface CoverStyleViewController ()<UITableViewDelegate, UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>{
    UITextField *numField;
}

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, assign) BOOL isNeed;

@property (nonatomic, strong) UIImagePickerController *pickerController;
@property (nonatomic, strong) FPCustomButton *addPhotoBtn;
@property (nonatomic, strong) UIImage        *addImage;

@end

@implementation CoverStyleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"版面样式";
    _isNeed = YES;
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    UIButton *connectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), ScreenX375(258), ScreenX375(49))];
    connectBtn.backgroundColor = REXADECIMALCOLOR(@"#ff9500");
    [connectBtn setTitle:@"有困难请联系我" forState:UIControlStateNormal];
    [connectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    connectBtn.titleLabel.font = Kfont(14);
    [self.view addSubview:connectBtn];
    
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(258), SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH - ScreenX375(258), ScreenX375(49))];
    nextBtn.backgroundColor = REXADECIMALCOLOR(@"#ed223b");
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.titleLabel.font = Kfont(14);
    [nextBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextBtn];
}

- (void)nextStep {
    EvaluateViewController *vc = [[EvaluateViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *cellId = @"imageCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(100), ScreenX375(45))];
            noteLabel.text = @"版面文件：";
            noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
            noteLabel.font = Kfont(14);
            [cell addSubview:noteLabel];
            
            _addPhotoBtn = [FPCustomButton buttonWithType:UIButtonTypeCustom];
            _addPhotoBtn.frame = CGRectMake(ScreenX375(86), ScreenX375(15), ScreenX375(110), ScreenX375(110));
            [_addPhotoBtn setBackgroundImage:[UIImage imageNamed:@"虚线边框"] forState:UIControlStateNormal];
            [_addPhotoBtn setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
            [_addPhotoBtn setTitle:@"添加图片" forState:UIControlStateNormal];
            [_addPhotoBtn setTitleColor:RGB_COLOR(210,210,210) forState:UIControlStateNormal];
            _addPhotoBtn.titleLabel.font = Kfont(14);
            [_addPhotoBtn addTarget:self action:@selector(addPhotoClick) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:_addPhotoBtn];
        }
        return cell;
    }else if (indexPath.row == 1){
        static NSString *cellId = @"numCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(100), ScreenX375(30))];
            noteLabel.text = @"数       量：";
            noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
            noteLabel.font = Kfont(14);
            [cell addSubview:noteLabel];
            
            UIView *operateView = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(87), 0, ScreenX375(109), ScreenX375(30))];
            operateView.layer.cornerRadius = ScreenX375(15);
            operateView.layer.borderWidth = 0.8;
            operateView.layer.borderColor = ThemeColor.CGColor;
            [cell addSubview:operateView];
            
            UIButton *subtractBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(ScreenX375(33)), ScreenX375(30))];
            [subtractBtn setTitle:@"-" forState:UIControlStateNormal];
            [subtractBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
            subtractBtn.titleLabel.font = Kfont(18);
            [subtractBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
            [operateView addSubview:subtractBtn];
            
            numField = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(33), 0, ScreenX375(43), ScreenX375(30))];
            numField.textAlignment = NSTextAlignmentCenter;
            numField.font = Kfont(13);
            numField.layer.borderWidth = 0.8;
            numField.text = @"1";
            numField.layer.borderColor = ThemeColor.CGColor;
            numField.keyboardType = UIKeyboardTypeNumberPad;
            numField.delegate = self;
            [operateView addSubview:numField];
            
            UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(76), 0, ScreenX375(ScreenX375(33)), ScreenX375(30))];
            [addBtn setTitle:@"+" forState:UIControlStateNormal];
            [addBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
            addBtn.titleLabel.font = Kfont(18);
            [addBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
            [operateView addSubview:addBtn];
        }
        return cell;
    }else if (indexPath.row == 2) {
        static NSString *cellId = @"needCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 0, ScreenX375(100), ScreenX375(30))];
            noteLabel.text = @"需要设计：";
            noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
            noteLabel.font = Kfont(14);
            [cell addSubview:noteLabel];
            
            UIButton *needBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(87), 0, ScreenX375(109), ScreenX375(30))];
            [needBtn setTitleColor:REXADECIMALCOLOR(@"#dcdcdc") forState:UIControlStateNormal];
            [needBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
            [needBtn setTitle:@"需要" forState:UIControlStateNormal];
            needBtn.layer.cornerRadius = ScreenX375(15);
            needBtn.layer.borderColor = ThemeColor.CGColor;
            needBtn.layer.borderWidth = 0.8;
            needBtn.titleLabel.font = Kfont(14);
            needBtn.tag = 555;
            needBtn.selected = YES;
            [needBtn addTarget:self action:@selector(need:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:needBtn];
            
            UIButton *notNeedBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(206), 0, ScreenX375(109), ScreenX375(30))];
            [notNeedBtn setTitleColor:REXADECIMALCOLOR(@"#dcdcdc") forState:UIControlStateNormal];
            [notNeedBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
            [notNeedBtn setTitle:@"不需要" forState:UIControlStateNormal];
            notNeedBtn.layer.cornerRadius = ScreenX375(15);
            notNeedBtn.layer.borderColor = REXADECIMALCOLOR(@"#dcdcdc").CGColor;
            notNeedBtn.layer.borderWidth = 0.8;
            notNeedBtn.titleLabel.font = Kfont(14);
            notNeedBtn.tag = 666;
            [notNeedBtn addTarget:self action:@selector(need:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:notNeedBtn];
            
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(45) - 0.5, SCREEN_WIDTH - ScreenX375(26), 0.5)];
            line.image = [UIImage imageNamed:@"虚线"];
            [cell addSubview:line];
        }
        return cell;
    }else {
        static NSString *cellId = @"selectCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            UIImageView *selectState = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15.5), ScreenX375(14), ScreenX375(14))];
            selectState.image = [UIImage imageNamed:@"选中"];
            selectState.tag = 444;
            [cell addSubview:selectState];
            
            UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(37), 0, ScreenX375(100), ScreenX375(45))];
            noteLabel.text = @"选择设计师";
            noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
            noteLabel.font = Kfont(14);
            noteLabel.tag = 333;
            [cell addSubview:noteLabel];
            
            UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(45))];
            [selectBtn addTarget:self action:@selector(selectDesigner) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:selectBtn];
            
            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(45) - 0.5, SCREEN_WIDTH, 0.5)];
            line.backgroundColor = LineColor;
            [cell addSubview:line];
        }
        return cell;
    }
}

- (void)selectDesigner {
    UIButton *btn = [self.view viewWithTag:555];
    if (btn.isSelected) {
        SelectDesignerViewController *vc = [[SelectDesignerViewController alloc] init];
        vc.chooseDesignerClick = ^(NSString *shopId, NSString *userId) {
            
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)delete {
    NSInteger num = [numField.text integerValue];
    if (num == 1) {
        [HttpManager showNoteMsg:@"数量至少为1"];
    }else {
        numField.text = [NSString stringWithFormat:@"%ld",num - 1];
    }
}

- (void)add {
    NSInteger num = [numField.text integerValue];
    numField.text = [NSString stringWithFormat:@"%ld",num + 1];
}

- (void)need:(UIButton *)button {
    if (button.tag == 555) {
        button.selected = YES;
        button.layer.borderColor = ThemeColor.CGColor;
        UIButton *btn = [self.view viewWithTag:666];
        btn.selected = NO;
        btn.layer.borderColor = REXADECIMALCOLOR(@"#dcdcdc").CGColor;
        UIImageView *imageView = [self.view viewWithTag:444];
        imageView.image = [UIImage imageNamed:@"选中"];
        UILabel *label = [self.view viewWithTag:333];
        label.textColor = FONTCOLOR_BLACK;
    }else {
        button.selected = YES;
        button.layer.borderColor = ThemeColor.CGColor;
        UIButton *btn = [self.view viewWithTag:555];
        btn.selected = NO;
        btn.layer.borderColor = REXADECIMALCOLOR(@"#dcdcdc").CGColor;
        UIImageView *imageView = [self.view viewWithTag:444];
        imageView.image = [UIImage imageNamed:@"未选中"];
        UILabel *label = [self.view viewWithTag:333];
        label.textColor = FONTCOLOR_GRAY;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return ScreenX375(140);
    }
    return ScreenX375(45);
}

- (void)addPhotoClick {
    if (!self.pickerController) {
        self.pickerController = [[UIImagePickerController alloc] init];
        self.pickerController.delegate = self;
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *send = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        self.pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }];
    UIAlertAction *get = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        self.pickerController.allowsEditing = YES;
        [self presentViewController:self.pickerController animated:YES completion:nil];
    }];
    
    [alert addAction:cancel];
    [alert addAction:send];
    [alert addAction:get];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [_addPhotoBtn setBackgroundImage:image forState:UIControlStateNormal];
    [_addPhotoBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_addPhotoBtn setTitle:@"" forState:UIControlStateNormal];
    _addImage = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self.pickerController dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
